<script setup>
import {ref, onMounted, reactive} from "vue";
import {useCaseStore} from "@/stores/caseStore.js";
import router from "@/router/index.js";

const caseStore = useCaseStore();

const caseList = ref([])

const form = reactive({
  caseName:'',
  status:'',
  caseType:''
})


onMounted(()=>{
  handleSearch()
})

const handleDetail = (row) => {
  console.log('row=',row)
  //检查一下 caseStore.currentCase 为什么是NULL
  caseStore.setCase(row);
  router.push(`/caseEdit/${row.id}`)
}

const handleSearch = () => {
  caseStore.fetchCases({
    caseName:form.caseName,
    status:form.status,
    caseType:form.caseType
  })
}

const formatStatus = (status) => {
  //'draft', 'reviewing', 'approved', 'rejected', 'processing', 'completed'
  const statusMap = {
    'draft': '草稿',
    'reviewing': '审核中',
    'approved': '已通过',
    'rejected': '已拒绝',
    'processing': '处理中',
    'completed': '已完成'
  }
  return statusMap[status]
}

const formatCaseType = (caseType) => {
  //1:民事案件,2:刑事案件
  const statusMap = {
    'draft': 'info',
    'reviewing': 'primary',
    'approved': 'success',
    'rejected': 'warning',
    'processing': 'success',
    'completed': 'success'
  }
  return statusMap[caseType]
}


</script>

<template>
  <div>
    <div>
      <el-form>
        <el-row>
          <el-col :span="6">
            <el-form-item>
              <el-input v-model="form.caseName" placeholder="请输入案件名称" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-select v-model="form.status" placeholder="请选择案件状态">
                <el-option label="进行中" value="1" />
                <el-option label="已完成" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-select v-model="form.caseType" placeholder="请选择案件类型">
                <el-option label="民事案件" value="1" />
                <el-option label="刑事案件" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <el-table :data="caseStore.cases" border>
      <el-table-column prop="caseName" label="案件名称" />
      <el-table-column prop="status" label="案件状态" >
        <template v-slot="scope">
          <el-tag :type="formatCaseType(scope.row.status)">{{formatStatus(scope.row.status)}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="caseType" label="案件类型" >
        <template v-slot="scope">
          {{scope.row.caseType === 1 ? '民事案件' : '刑事案件'}}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="申请时间" />
      <el-table-column prop="realName" label="申请人" />
      <el-table-column label="操作" width="300">
        <template v-slot="scope">
          <el-button type="primary" size="small" @click="handleDetail(scope.row)">查看详情</el-button>
          <el-button type="warning" size="small" @click="handleDetail(scope.row)">删除</el-button>
          <el-button type="success" size="small" @click="handleDetail(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped>

</style>